package eto;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EtoServlet extends HttpServlet {
	final String[]etos = {"子","丑","寅","卯","辰","巳",
						  "午","未","申","酉","戌","亥"};
	@Override
	protected void doPost(HttpServletRequest req, 
						  HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req,resp);
	}
	@Override
	protected void doGet(HttpServletRequest req,
						HttpServletResponse resp)
			throws ServletException, IOException {
	
		String strYear = req.getParameter("year");
		
		//コンテンツタイプの指定
		resp.setContentType("text/html;charset=UTF-8");

		PrintWriter out =resp.getWriter();
		out.println("<p>");
		out.println("<a href='/SecondApp/'>&nbsp;戻る</a>");
		out.println("</p>");
		
		if(strYear == null || strYear.length() == 0 ){
			out.println("西暦で年を入力してください。");
		}
		else{
			try{
			int year = Integer.parseInt(strYear);
			int index = (year + 8)% 12;
			String eto = etos[index];
			out.println(eto + "年です。");
			}
			catch(NumberFormatException e){
				out.println("西暦で年を入力してください。");
			}
		}
	}
}
